package com.egene.tw.activity.member;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.egene.tw.R;
import com.egene.tw.activity.wiki.WikInfoActivity;
import com.egene.tw.activity.wiki.WikInfoActivity_;
import com.egene.tw.api.EgeneApi;
import com.egene.tw.base.BaseRecycleViewFragment;
import com.egene.tw.base.RecycleBaseAdapter;
import com.egene.tw.dao.UserDao;
import com.egene.tw.model.HealthItem;
import com.egene.tw.model.HealthList;
import com.egene.tw.model.HealthListItem;
import com.egene.tw.model.Suggest;
import com.egene.tw.model.net.ResponseList;
import com.egene.tw.utils.JsonUtil;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

@EFragment
public class HealthListFragment extends BaseRecycleViewFragment {

    public static final String TYPE = "TYPE";
    private String type;

    @Bean
    UserDao userDao;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getString(TYPE);
        }

    }

    @Override
    protected boolean isNeedListDivider() {
        return true;
    }

    @Override
    protected RecycleBaseAdapter getListAdapter() {
        return new HealthRecycleAdapter();
    }


    @Override
    protected void sendRequestData() {
        fetchList();
    }

    void fetchList() {
        if ("4".equals(type)) {
            EgeneApi.getGetPersonOrderList(userDao.getUser().getUserID(), new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    HealthList result = JsonUtil.parse(responseBody, HealthList.class);
                    if (result != null) {
                        if (result.isSuccess()) {
                            List<HealthListItem> l = tr(result.getDetaillist(), result.getSuggestlist());
                            updateList(l);
                        }
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//                System.out.println(new String(responseBody));
                }
            });
        } else {
            EgeneApi.getHealthListByType(userDao.getUser().getUserID(), type, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                    ResponseList<HealthListItem> response = JsonUtil.toList(responseBody,
                            new TypeToken<ResponseList<HealthListItem>>() {
                            }.getType());
                    if (response != null) {
//                    System.out.println(response);
                        updateList(response.getData());
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//                System.out.println(new String(responseBody));
                }
            });
        }

    }

    private List<HealthListItem> tr(List<HealthItem> list, List<Suggest> suggestList) {
        List<HealthListItem> listItems = new ArrayList<>();
        if (list == null) {
            return listItems;
        }
        HealthListItem title = new HealthListItem();
        title.setTitle("基因檢測保健計劃");
        title.setViewType(HealthRecycleAdapter.TITLE);
        listItems.add(title);

        if(list.size() != 0){
            for (HealthItem item : list) {
                HealthListItem listItem = new HealthListItem();
                listItem.setViewType(HealthRecycleAdapter.CONTENT);
                listItem.setRid(item.getRid());
                listItem.setTitle(item.getTitle()+"-預防保健計劃");
                listItem.setZhaiyao(item.getCareremark());
                listItem.setOrderId("" + item.getOrderid());
                listItem.setImg_url(item.getSmallimg());
                listItem.setNoImage(true);
                listItems.add(listItem);
            }
        }

        HealthListItem title1 = new HealthListItem();
        title1.setTitle("整合性建議");
        title1.setViewType(HealthRecycleAdapter.TITLE);
        listItems.add(title1);

        if(suggestList != null && suggestList.size() != 0){
            for (Suggest suggest : suggestList) {
                HealthListItem listItem = new HealthListItem();
                listItem.setViewType(HealthRecycleAdapter.CONTENT);
                listItem.setTitle(suggest.getShortContent());
                listItem.setZhaiyao(suggest.getContent());
                listItem.setSuggest(true);
                listItem.setNoImage(true);
                listItems.add(listItem);
            }
        }

        return listItems;
    }


    @Override
    public void loadMore() {
//        super.loadMore();
    }

    @UiThread
    void updateList(List<HealthListItem> list) {
        if (mCurrentPage == START_PAGE_INDEX) {
            mAdapter.clear();
        }
        mAdapter.addData(list);
        mAdapter.notifyDataSetChanged();
        executeOnLoadFinish();
    }

    @Override
    protected void onItemClick(View view, int position) {
        HealthListItem item = (HealthListItem) mAdapter.getItem(position);
        if(item.getViewType() == HealthRecycleAdapter.TITLE){
            return;
        }
        if(item.isSuggest()){
            TextActivity_.intent(getActivity()).title("详情").content(item.getZhaiyao()).start();
            return;
        }

        if (item.isNoImage()) {
            MoreDetailActivity_.intent(getActivity()).rid(item.getRid()).orderId(item.getOrderId()).start();
        } else {
            Intent intent = new Intent(getActivity(), WikInfoActivity_.class);
            intent.putExtra(WikInfoActivity.ID, "" + item.getId());
            startActivity(intent);
        }
//        Intent intent = new Intent(getActivity(), CategoryInfoActivity_.class);
//        intent.putExtra(CategoryInfoActivity.ID, "" + category.getId());
//        startActivity(intent);
    }
}
